﻿using Raffles.OA.Data;
using Raffles.OA.Utility;
using Raffles.OA.WebApp.App_Filter;
using SqlSugar;
using System;
using System.IO;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.App_Start
{ 
    public class BaseActionController : Controller
    {
        public static SqlSugarScope _dbContext = ReportDB.Db;

        #region 视图 
        public virtual ActionResult Index()
        {
            return View();
        } 
        public virtual ActionResult Form()
        {
            return View();
        } 
        public virtual ActionResult Detail()
        {
            return View();
        }
        #endregion

        #region 文件导出

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>

        [HttpGet]
        public FileContentResult ExportFile(string filePath)
        {
            try
            {
                DataResult<FileContentResult> obj = new DataResult<FileContentResult>();
                byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
                string fileName = Path.GetFileName(filePath);
                string fileExtensionName = Path.GetExtension(filePath);
                obj.data = new FileContentResult(fileBytes, "application/octet-stream")
                {
                    FileDownloadName = Path.GetFileName(filePath)
                };
                return obj.data;
            }
            catch (Exception ex)
            {
                throw new Exception("下载失败:" + ex.Message);
            }
            finally
            {
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
            }
        }

        #endregion 文件导出 
    }
} 